package com.phil.tuangou.main.activtiy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.phi.tuangou.bean.CityBean;
import com.phi.tuangou.bean.LashouDetailBean;
import com.phil.tuangou.lashou.MyLaShouNet;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainAcitivity extends Activity implements OnItemClickListener {
	/** Called when the activity is first created. */
	private static final int SELECTED_CITY_HANDLER = 1;
	Spinner selectCitySp;
	ListView mainGuangouInfoLV;
	ArrayAdapter<CityBean> cityAdapter;
	List<CityBean> citysList = new ArrayList<CityBean>();
	
	ITuanGou lashouObj;
	private CityBean currentCity;
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SELECTED_CITY_HANDLER:
				getCityDetailTG();
				break;

			default:
				break;
			}
		}
	};
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initGUI();
	}

	public void initGUI() {
		lashouObj = new MyLaShouNet();
		selectCitySp = (Spinner) findViewById(R.id.selectcitysp);
		mainGuangouInfoLV = (ListView) findViewById(R.id.mainListview);
		mainGuangouInfoLV.setOnItemClickListener(this);

		// ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
		// this, R.array.citys, android.R.layout.simple_spinner_item);

		cityAdapter = new ArrayAdapter<CityBean>(this,
				android.R.layout.simple_spinner_item, citysList);
		cityAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		selectCitySp.setAdapter(cityAdapter);
		selectCitySp.setOnItemSelectedListener(new OnSpinnerSelectedListener());

		AsynRequestThread requestThread = new AsynRequestThread(lashouObj);
		requestThread.requestCitys(new RequestListener() {
			
			public void onSuccuss(List list) {
				// TODO Auto-generated method stub
			
				for(int i = 0;  i < list.size();i++){
					CityBean cityBean = (CityBean) list.get(i);
					citysList.add(cityBean);
					
				}
				cityAdapter.notifyDataSetChanged();
			}
			
			public void onFail() {
				// TODO Auto-generated method stub
				
			}
			
			public void onExcepiton() {
				// TODO Auto-generated method stub
				
			}
		});
	}

	// mainlist click
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

	}

	// spinner event handler
	private class OnSpinnerSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
			Toast.makeText(MainAcitivity.this, "Spinner1: position=" + position
					+ " id=" + id, 2000).show();
			currentCity =  citysList.get(position);
			handler.sendEmptyMessage(SELECTED_CITY_HANDLER);
		}

		public void onNothingSelected(AdapterView<?> parent) {
			
		}

	}

	private void getCityDetailTG(){
		try {
			List<LashouDetailBean> list = lashouObj.getInfo(currentCity);
			System.out.println("city detail size = "+ list.size());
			for(LashouDetailBean bean : list){
					System.out.println(bean.getCityid());
					System.out.println(bean.getCityname());
					System.out.println(bean.getGoods_bought());
					System.out.println(bean.getGoods_title());
				}
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}